#pragma once
#include <iostream>
#include <pthread.h>
#include "myh/MyLog.hpp"
class PthMutex
{
public:
    PthMutex()  
        :_lock(new pthread_mutex_t)
    {
        int ret = pthread_mutex_init(_lock,nullptr);
        HandleRet("pthread_mutex_init",ret);
    }
    void lock()
    {
        int ret = pthread_mutex_lock(_lock);
        HandleRet("pthread_mutex_lock",ret);
    }
    void unlock()
    {
        int ret = pthread_mutex_unlock(_lock);
        HandleRet("pthread_mutex_unlock",ret);
    }
    ~PthMutex()
    {
        int ret = pthread_mutex_destroy(_lock);
        HandleRet("pthread_mutex_destroy",ret);
        delete _lock;
    }
private:
    void HandleRet(const std::string& funname,int _error)
    {
        if(_error)
        {
            lg.defaultprint("funname",_error);
        }
    }
    pthread_mutex_t* _lock;
};

class Guardlock
{
public:
    Guardlock()
        :_plock(new PthMutex)
    {
        _plock->lock();
    }
    ~Guardlock()
    {
        _plock->unlock();
    }
private:
    PthMutex* _plock;
};